import {Component, Input, OnInit} from '@angular/core';
import {Router} from "@angular/router";
import {CurrUserService} from "../service/curr-user.service";
import {CurrUserModel} from "../models/curr-user.model";
import {fadeInAnimation} from "../../routing/route.animation";
import {UsersService} from "../service/users.service";
import {MdDialog} from "@angular/material";

@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.scss'],
  host: {
    '[@fadeInAnimation]': 'true'
  },
  animations: [ fadeInAnimation ],
  providers:[UsersService]
})
export class HeaderComponent implements OnInit {

  @Input() needRouter:boolean = true;

  currUser:CurrUserModel =  new CurrUserModel(null,null,null,null);
  constructor( private router: Router,
               private userService:UsersService,
               private currUserService:CurrUserService,
               private dialog:MdDialog,
                  ) {}

  ngOnInit() {
    this.currUserService.currUserChange.subscribe((currUserModel:CurrUserModel) => {
      this.currUser = currUserModel;
      console.log(this.currUser)
    })
    this.userService.loadCurrentUserInfo().then((user)=>{
      this.currUserService.setCurrUser( new CurrUserModel(user.id,user.realName,user.groupId,user.roleName))
    },error=>{
      console.error("获取当前用户出错：",error);
    })
  }
  onActivate(e, scrollContainer) {
    scrollContainer.scrollTop = 0;
  }

  goHome(e){
    this.dialog.closeAll();
    this.router.navigate(['auth-guard/home']);
  }
}
